package com.mcintyret.site.web.controller.journal;

import com.mcintyret.site.journal.Datapoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ValueConstants;

import java.util.Date;

/**
 * User: mcintyret2
 * Date: 06/04/2012
 */

@Controller
public class DatapointController extends BaseJournalController {

    @RequestMapping(value = "/add-datapoint", method = RequestMethod.POST)
    private String addDatapointToNumericalSequence(
            @RequestParam("value") double value,
            @RequestParam("datetime") String timeInMillis,
            @RequestParam(required = false, value = "comment") String comment,
            @RequestParam("timeseriesId") int timeseriesId) {

        Datapoint newDatapoint = new Datapoint(value);
        try {
            newDatapoint.setTime(new Date(Long.parseLong(timeInMillis)));
        } catch (NumberFormatException nfe) {
            // Do nothing - date stays as now.
        }
        if (comment != null && !comment.equals(ValueConstants.DEFAULT_NONE)) {
            newDatapoint.setComment(comment);
        }

        journalDao.addNewDatapointToNumericalSequenceWithId(timeseriesId, newDatapoint);

        return "redirect:timeseries?id=" + timeseriesId;
    }
}
